package com.fortune.controller;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.xml.crypto.Data;

import com.fortune.base.BaseController;
import com.fortune.dto.excution.WeChatReturnExcution;
import com.fortune.entity.WeChatReturnClient;
import com.fortune.enums.WeChatReturnEnum;
import com.fortune.service.WeChatReturnService;
import com.fortune.vo.wechatReturn.ReturnClient;
import com.fortune.vo.wechatReturn.ReturnVisitVo;
import io.swagger.annotations.ApiImplicitParams;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fortune.base.BaseAPIExcution;
import com.fortune.vo.wechatReturn.ReturnClientsVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Api(value = "wechat/return", description = "回访相关", tags = {"微信小程序：回访列表"})
@RestController
@RequestMapping("wechat/return")
@Validated
public class WeChatReturnController extends BaseController{
	@Autowired
	private WeChatReturnService weChatReturnService;

	@ApiOperation(value = "回访列表", nickname = "returnList", notes = "", tags={ "@郭阳" })
	@ApiImplicitParams({
			@ApiImplicitParam(name="type", value="状态：1:待回访，2:已停用，3:全部",paramType="query",dataType="int",allowableValues="1,2,3"),
			@ApiImplicitParam(name="Authorization", value="登录返回的token值",paramType="header",dataType="String",required = true)
	})
    @GetMapping(value = "list",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<ReturnVisitVo> list(@Validated @Min(value=1,message="类型只能为1,2,3") @Max(value=3,message="类型只能为1,2,3")int type) {
		String did = getDId();
		String duid = getUserId();
		if(type == 1){//正常
			List<WeChatReturnClient> findAllListReturnClientByStatusYes = weChatReturnService.findAllListReturnClientByStatusYes(did, duid);
			List<ReturnClient> expired = new ArrayList<>();//已过期
			List<ReturnClient> returnToday = new ArrayList<>();//今日回访
			List<ReturnClient> returnOneDay = new ArrayList<>();//一天后
			List<ReturnClient> laterVsits = new ArrayList<>();//以后
			ReturnClient returnClient = new ReturnClient();
			for (int i = 0; i < findAllListReturnClientByStatusYes.size(); i++){
				WeChatReturnClient weChatReturnClient = findAllListReturnClientByStatusYes.get(i);
				Date lastTime = weChatReturnClient.getLastTime();
				float returnPeriod = weChatReturnClient.getReturnPeriod();
				long lastTimes = lastTime.getTime();//上次回访时间
				long returnPeriods = (long)returnPeriod * 86400 * 1000;//回访周期
				Date day=new Date();
				SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				long nowDate = day.getTime();//获取当前时间
				long nowLastTime = lastTimes + returnPeriods;//下一次回访时间
				if(nowDate > nowLastTime){//已过期
					String id = weChatReturnClient.getId();
					String name = weChatReturnClient.getName();
					String number = weChatReturnClient.getNumber();
					Date lastTimeA = weChatReturnClient.getLastTime();
					long expiredDay = (nowDate - nowLastTime)/86400;
					returnClient.setId(id);
					returnClient.setName(name);
					returnClient.setNumber(number);
					returnClient.setLastTime(lastTimeA);
					returnClient.setExpiredDay(expiredDay);
					returnToday.add(returnClient);
				}
				long days = returnPeriods - (nowDate - lastTimes);
				if(days > 0 && days < 1){//今日回访
					String id = weChatReturnClient.getId();
					String name = weChatReturnClient.getName();
					String number = weChatReturnClient.getNumber();
					Date lastTimeA = weChatReturnClient.getLastTime();
					long expiredDay = (nowDate - nowLastTime)/86400;
					returnClient.setId(id);
					returnClient.setName(name);
					returnClient.setNumber(number);
					returnClient.setLastTime(lastTimeA);
					returnClient.setExpiredDay(expiredDay);
					expired.add(returnClient);
				}
				if(1< days && days < 2){//一天后
					String id = weChatReturnClient.getId();
					String name = weChatReturnClient.getName();
					String number = weChatReturnClient.getNumber();
					Date lastTimeA = weChatReturnClient.getLastTime();
					long expiredDay = (nowDate - nowLastTime)/86400;
					returnClient.setId(id);
					returnClient.setName(name);
					returnClient.setNumber(number);
					returnClient.setLastTime(lastTimeA);
					returnClient.setExpiredDay(expiredDay);
					returnOneDay.add(returnClient);
				}
				if(days >= 2){//以后
					String id = weChatReturnClient.getId();
					String name = weChatReturnClient.getName();
					String number = weChatReturnClient.getNumber();
					Date lastTimeA = weChatReturnClient.getLastTime();
					long expiredDay = (nowDate - nowLastTime)/86400;
					returnClient.setId(id);
					returnClient.setName(name);
					returnClient.setNumber(number);
					returnClient.setLastTime(lastTimeA);
					returnClient.setExpiredDay(expiredDay);
					laterVsits.add(returnClient);
				}
			}
			ReturnVisitVo returnVisitVo = new ReturnVisitVo();
			returnVisitVo.setExpired(expired);
			returnVisitVo.setLaterVsits(laterVsits);
			returnVisitVo.setReturnOneDay(returnOneDay);
			returnVisitVo.setReturnToday(returnToday);
			return new WeChatReturnExcution<ReturnVisitVo> (returnVisitVo, WeChatReturnEnum.FIND_SUCCESS);
		}
		if(type == 2){//停用
			List<WeChatReturnClient> findAllListReturnClientByStatusYes = weChatReturnService.findAllListReturnClientByStatusYes(did, duid);
			List<ReturnClient> expired = new ArrayList<>();//已过期
			List<ReturnClient> returnToday = new ArrayList<>();//今日回访
			List<ReturnClient> returnOneDay = new ArrayList<>();//一天后
			List<ReturnClient> laterVsits = new ArrayList<>();//以后
			ReturnClient returnClient = new ReturnClient();
			for (int i = 0; i < findAllListReturnClientByStatusYes.size(); i++){
				WeChatReturnClient weChatReturnClient = findAllListReturnClientByStatusYes.get(i);
				Date lastTime = weChatReturnClient.getLastTime();
				float returnPeriod = weChatReturnClient.getReturnPeriod();
				long lastTimes = lastTime.getTime();//上次回访时间
				long returnPeriods = (long)returnPeriod * 86400 * 1000;//回访周期
				Date day=new Date();
				SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				long nowDate = day.getTime();//获取当前时间
				long nowLastTime = lastTimes + returnPeriods;//下一次回访时间
				if(nowDate > nowLastTime){//已过期
					String id = weChatReturnClient.getId();
					String name = weChatReturnClient.getName();
					String number = weChatReturnClient.getNumber();
					Date lastTimeA = weChatReturnClient.getLastTime();
					long expiredDay = (nowDate - nowLastTime)/86400;
					returnClient.setId(id);
					returnClient.setName(name);
					returnClient.setNumber(number);
					returnClient.setLastTime(lastTimeA);
					returnClient.setExpiredDay(expiredDay);
					returnToday.add(returnClient);
				}
				long days = returnPeriods - (nowDate - lastTimes);
				if(days < 0){//今日回访
					String id = weChatReturnClient.getId();
					String name = weChatReturnClient.getName();
					String number = weChatReturnClient.getNumber();
					Date lastTimeA = weChatReturnClient.getLastTime();
					long expiredDay = (nowDate - nowLastTime)/86400;
					returnClient.setId(id);
					returnClient.setName(name);
					returnClient.setNumber(number);
					returnClient.setLastTime(lastTimeA);
					returnClient.setExpiredDay(expiredDay);
					expired.add(returnClient);
				}
				if(1< days && days < 2){//一天后
					String id = weChatReturnClient.getId();
					String name = weChatReturnClient.getName();
					String number = weChatReturnClient.getNumber();
					Date lastTimeA = weChatReturnClient.getLastTime();
					long expiredDay = (nowDate - nowLastTime)/86400;
					returnClient.setId(id);
					returnClient.setName(name);
					returnClient.setNumber(number);
					returnClient.setLastTime(lastTimeA);
					returnClient.setExpiredDay(expiredDay);
					returnOneDay.add(returnClient);
				}
				if(days >= 2){//以后
					String id = weChatReturnClient.getId();
					String name = weChatReturnClient.getName();
					String number = weChatReturnClient.getNumber();
					Date lastTimeA = weChatReturnClient.getLastTime();
					long expiredDay = (nowDate - nowLastTime)/86400;
					returnClient.setId(id);
					returnClient.setName(name);
					returnClient.setNumber(number);
					returnClient.setLastTime(lastTimeA);
					returnClient.setExpiredDay(expiredDay);
					laterVsits.add(returnClient);
				}
			}
			ReturnVisitVo returnVisitVo = new ReturnVisitVo();
			returnVisitVo.setExpired(expired);
			returnVisitVo.setLaterVsits(laterVsits);
			returnVisitVo.setReturnOneDay(returnOneDay);
			returnVisitVo.setReturnToday(returnToday);
			return new WeChatReturnExcution<ReturnVisitVo> (returnVisitVo, WeChatReturnEnum.FIND_SUCCESS);
		}
		if(type == 3){
			List<WeChatReturnClient> findAllListReturnClientByStatusYes = weChatReturnService.findAllListReturnClientByStatusYes(did, duid);
			List<ReturnClient> expired = new ArrayList<>();//已过期
			List<ReturnClient> returnToday = new ArrayList<>();//今日回访
			List<ReturnClient> returnOneDay = new ArrayList<>();//一天后
			List<ReturnClient> laterVsits = new ArrayList<>();//以后
			ReturnClient returnClient = new ReturnClient();
			for (int i = 0; i < findAllListReturnClientByStatusYes.size(); i++){
				WeChatReturnClient weChatReturnClient = findAllListReturnClientByStatusYes.get(i);
				Date lastTime = weChatReturnClient.getLastTime();
				float returnPeriod = weChatReturnClient.getReturnPeriod();
				long lastTimes = lastTime.getTime();//上次回访时间
				long returnPeriods = (long)returnPeriod * 86400 * 1000;//回访周期
				Date day=new Date();
				SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				long nowDate = day.getTime();//获取当前时间
				long nowLastTime = lastTimes + returnPeriods;//下一次回访时间
				if(nowDate > nowLastTime){//已过期
					String id = weChatReturnClient.getId();
					String name = weChatReturnClient.getName();
					String number = weChatReturnClient.getNumber();
					Date lastTimeA = weChatReturnClient.getLastTime();
					long expiredDay = (nowDate - nowLastTime)/86400;
					returnClient.setId(id);
					returnClient.setName(name);
					returnClient.setNumber(number);
					returnClient.setLastTime(lastTimeA);
					returnClient.setExpiredDay(expiredDay);
					returnToday.add(returnClient);
				}
				long days = returnPeriods - (nowDate - lastTimes);
				if(days < 0){//今日回访
					String id = weChatReturnClient.getId();
					String name = weChatReturnClient.getName();
					String number = weChatReturnClient.getNumber();
					Date lastTimeA = weChatReturnClient.getLastTime();
					long expiredDay = (nowDate - nowLastTime)/86400;
					returnClient.setId(id);
					returnClient.setName(name);
					returnClient.setNumber(number);
					returnClient.setLastTime(lastTimeA);
					returnClient.setExpiredDay(expiredDay);
					expired.add(returnClient);
				}
				if(1< days && days < 2){//一天后
					String id = weChatReturnClient.getId();
					String name = weChatReturnClient.getName();
					String number = weChatReturnClient.getNumber();
					Date lastTimeA = weChatReturnClient.getLastTime();
					long expiredDay = (nowDate - nowLastTime)/86400;
					returnClient.setId(id);
					returnClient.setName(name);
					returnClient.setNumber(number);
					returnClient.setLastTime(lastTimeA);
					returnClient.setExpiredDay(expiredDay);
					returnOneDay.add(returnClient);
				}
				if(days >= 2){//以后
					String id = weChatReturnClient.getId();
					String name = weChatReturnClient.getName();
					String number = weChatReturnClient.getNumber();
					Date lastTimeA = weChatReturnClient.getLastTime();
					long expiredDay = (nowDate - nowLastTime)/86400;
					returnClient.setId(id);
					returnClient.setName(name);
					returnClient.setNumber(number);
					returnClient.setLastTime(lastTimeA);
					returnClient.setExpiredDay(expiredDay);
					laterVsits.add(returnClient);
				}
			}
			ReturnVisitVo returnVisitVo = new ReturnVisitVo();
			returnVisitVo.setExpired(expired);
			returnVisitVo.setLaterVsits(laterVsits);
			returnVisitVo.setReturnOneDay(returnOneDay);
			returnVisitVo.setReturnToday(returnToday);
			return new WeChatReturnExcution<ReturnVisitVo> (returnVisitVo, WeChatReturnEnum.FIND_SUCCESS);
		}
		return new WeChatReturnExcution<ReturnVisitVo> (null, WeChatReturnEnum.FIND_RESULT_AIR);
	}
	
	@ApiOperation(value = "-	确认已回访", nickname = "returnConfirm", notes = "", tags={ "@郭阳" })
	@ApiImplicitParams({
			@ApiImplicitParam(name="clientId", value="客户id",paramType="query",dataType="string"),
			@ApiImplicitParam(name="Authorization", value="登录返回的token值",paramType="header",dataType="String",required = true)
	})
    @PostMapping(value = "confirm",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> confirm(@Validated @NotBlank(message="客户id不能为空") String clientId) {
		String did = getDId();
		int editLastReturnTime = weChatReturnService.editLastReturnTime(did, clientId);
		if(editLastReturnTime < 1){
			return new WeChatReturnExcution<Void> (null, WeChatReturnEnum.EDIT_ERROR);
		}
		return new WeChatReturnExcution<Void> (null, WeChatReturnEnum.EDIT_SUCCESS);
	}
}
